/**
 * 获得搜索参数
 * @param params
 * @returns {Object}
 */
function getSearchParams(params) {
    var searchParams = new Object();
    if (params != undefined) {
        searchParams = params;
    }
    $('.SearchForm').each(function () {
        var param = $(this).val().trim();
        if (param == undefined)
            param = '';
        searchParams[$(this).attr('id')] = param;
    });
    return searchParams;
}


/**
 * 页面自加载
 */
$(function () {
    $('#time').datepicker({
        format : "yyyy-mm-dd",
        todayBtn : "linked",
        autoclose : true,
        todayHighlight : true,
        clearBtn : true
    });
    var cellwidth = ($(".box-content.table-responsive").width() - 55) / 11;
    var psval = $('#datatable_noticeinfo').attr('data-size');
    if (psval == undefined || psval == "") {
        psval = 10;
    }
    /**
     * easyui 生成表格数据
     */
    $('#datatable_noticeinfo').datagrid({
        striped: true,
        remoteSort: false,
        // idField: 'team_id',
        collapsible: true,
        fit: false,
        url: 'super/Team/getTeamInfo',//数据源路径
        loadMsg: '请等待数据载入....',
        pagination: true,
        rownumbers: true,
        fitColumns: true,
        nowrap:false,
        singleSelect: true,
        pageSize: psval,
        pageList: [psval, psval * 2, psval * 3, psval * 4, psval * 5],
        columns: [[
            {
                field: 'team_name',
                title: '团队名称',
                align: 'center',
                width: cellwidth,
                sortable: true,
            }, {
                field: 'team_img',
                title: '团队头像',
                align: 'center',
                width: cellwidth,
                sortable: true,
                formatter:function(value,row,index){
                    if(value!=''){
                        return "<img src='"+row.team_img+"' style='width:100px;height:100px'>";
                    }
                }
            },{
                field: 'team_number',
                title: '团队编号',
                align: 'center',
                width: cellwidth,
                sortable: true,
            }
            ,{
                field: 'user_username',
                title: '团队负责人',
                align: 'center',
                width: cellwidth,
                sortable: true,
            }
            ,{
                field: 'team_explain',
                title: '职能说明',
                align: 'center',
                width: cellwidth,
                sortable: true,
                formatter:function(value){
                    return value.substring(0,30)+'...';

                }
            }
            ,{
                field: 'team_status',
                title: '认证状态',
                align: 'center',
                width: cellwidth,
                sortable: true,
                formatter:function(value){
                    if(value==1){
                        return "<span style='color:red;font-weight: bold'>未认证</span>";
                    }else if(value==2){
                        return "<span style='color:blueviolet;font-weight: bold'>已认证</span>";
                    }else if(value==3){
                        return "<span style='color:rebeccapurple;font-weight: bold'>未通过</span>";
                    }else{
                        return "<span style='color:royalblue;font-weight: bold'>认证中</span>";
                    }
                }
            }
            ,{
                field: 'username',
                title: '审核人',
                align: 'center',
                width: cellwidth,
                sortable: true,
            }
            ,{
                field: 'member_count',
                title: '团队人数',
                align: 'center',
                width: cellwidth,
                sortable: true,
            }
            ,{
                field: 'Relation_count',
                title: '关联团队数',
                align: 'center',
                width: cellwidth,
                sortable: true,
            }
            ,{
                field: 'Adv_count',
                title: '已发公众信息数',
                align: 'center',
                width: cellwidth,
                sortable: true,
            }
            ,{
                field: 'Assignment_count',
                title: '任务数',
                align: 'center',
                width: cellwidth,
                sortable: true,
            }
            ,{
                field: 'status',
                title: '状态',
                align: 'center',
                width: cellwidth,
                sortable: true,
                formatter:function(value){
                    if(value==1){
                        return "<span style='color:red;font-weight: bold'>启用</span>";
                    }else{
                        return "<span style='color:rebeccapurple;font-weight: bold'>停用</span>";

                    }
                }
            }
            ,{
                field: 'team_time',
                title: '创建时间',
                align: 'center',
                width: cellwidth,
                sortable: true,
            }
            ,{
                field: 'team_id',
                title: '操作',
                align: 'center',
                width: cellwidth,
                sortable: true,
                formatter:function(value,row,index){
                    var html ='';
                        if(row.team_status==4){
                            html ="<a onclick='ShReleaseInfo(this);' style='color:green;text-decoration:none;' name='"+value+"' href='JavaScript:;'>审核</a><br/>";

                        }
                    if(row.status==1){
                        html +="<a onclick='TyReleaseInfo(this);' style='color:red;text-decoration:none' name='"+value+"' href='JavaScript:;'>停用</a><br/>";
                    }else{
                        html +="<a onclick='QyReleaseInfo(this);' style='color:brown;text-decoration:none' name='"+value+"' href='JavaScript:;'>启用</a><br/>";
                    }
                    html +="<a onclick='CxReleaseInfo(this);' style='color:#F75000;text-decoration:none' name='"+value+"' href='JavaScript:;'>查看关联团队</a><br/>";
                   // html +="<a onclick='UpdateTeamInfo(this);' style='color:black;text-decoration:none' name='"+value+"' href='JavaScript:;'>编辑团队</a><br/>";
                    return html;
                }
            }
        ]],
        onBeforeLoad: function (param) {
            param = getSearchParams(param);
        },
    });

//搜索
    $('#Search').click(function () {
        $('#datatable_noticeinfo').datagrid('reload');
    });
    //清空搜索条件
    $("#Reset").click(function() {
        $('.SearchForm').val('');
    });
    //添加
    $('.addbook').click(function () {
        var content = "";
        $.ajax({
            url: 'super/Team/addTeam',
            type: 'POST',
            dataType: 'HTML',//返回的数据类型
            success: function (updatemodalhtml) {
                content = updatemodalhtml;
                //弹出添加框
                $.TeachDialog({
                    title: '新增团队信息',
                    content: content,
                    largeSize: true,
                    showCloseButton: true,
                    showCloseButtonName: '关闭',
                    otherButtons: ['保存'],
                    otherButtonStyles: ['btn-primary'],
                    clickButton: function (sender, modal, index) {
                        var formData = new FormData($("#updateForm" )[0]);
                        $.ajax({
                            url: 'super/Team/insertTeam',
                            data: formData,
                            type: 'POST',
                            dataType: 'JSON',
                            async: false,
                            cache: false,
                            contentType: false,
                            processData: false,
                            success: function (returnData) {
                                if (returnData.status == 0) {
                                    // $.TeachDialog({
                                    //
                                    //     content: returnData.msg
                                    // });
                                    layer.open({
                                        content: returnData.msg
                                    });
                                } else {
                                    modal.modal('hide');
                                    // $.TeachDialog({
                                    //     content: returnData.msg
                                    // });
                                    $('#datatable_noticeinfo').datagrid('reload');
                                }
                            },
                            error: function () {
                                $.TeachDialog({
                                    content: '系统异常，请联系管理员',
                                });
                            },

                        });

                    },
                });
            },
            error: function () {
                $.TeachDialog({
                    content: '系统异常，请联系管理员',
                });
                return;
            }
        });
    });

    //编辑
    $('.editbook').click(function () {
        var row = $('#datatable_noticeinfo').datagrid('getSelections');
        if (row.length<1) {
            $.TeachDialog({
                content: '请选择一行数据进行修改！',
            });
            return;
        }
        if (row.length>1) {
            $.TeachDialog({
                content: '只能选择一行进行修改！',
            });
            return;
        }
        var content = "";
        $.ajax({
            url: 'super/Team/updateTeamRes',
            data: "nid=" + row[0].team_id,
            type: 'POST',
            dataType: 'HTML',//返回的数据类型
            success: function (updatemodalhtml) {
                content = updatemodalhtml;
                //弹出修改框
                $.TeachDialog({
                    title: '修改团队信息',
                    content: content,
                    showCloseButton: true,
                    showCloseButtonName: '关闭',
                    otherButtons: ['保存'],
                    otherButtonStyles: ['btn-primary'],
                    clickButton: function (sender, modal, index) {
                        var formData = new FormData($("#updateForm" )[0]);
                        $.ajax({
                            url: 'super/Team/insertTeamInfo',
                            data: formData,
                            type: 'POST',
                            dataType: 'JSON',
                            async: false,
                            cache: false,
                            contentType: false,
                            processData: false,
                            success: function (returnData) {
                                if (returnData.status == 0) {
                                    // $.TeachDialog({
                                    //     content: returnData.msg
                                    // });
                                    layer.open({
                                        content: returnData.msg
                                    });
                                } else {
                                    modal.modal('hide');
                                    $('#datatable_noticeinfo').datagrid('reload');
                                }
                            },
                            error: function () {
                                $.TeachDialog({
                                    content: '系统异常，请联系管理员'
                                });
                            },

                        });

                    },
                });
            },
            error: function () {
                $.TeachDialog({
                    content: '系统异常，请联系管理员'
                });
                return;
            }
        });
    })
    /***
     * 关联团队
     */
    $('.relation').click(function () {
        var row = $('#datatable_noticeinfo').datagrid('getSelections');
        if (row.length<1) {
            $.TeachDialog({
                content: '请选择一行数据进行关联！',
            });
            return;
        }
        if (row.length>1) {
            $.TeachDialog({
                content: '只能选择一行进行关联！',
            });
            return;
        }
        var content = "";
        $.ajax({
            url: 'super/Team/relationTeam',
            data: "nid=" + row[0].team_id,
            type: 'POST',
            dataType: 'HTML',//返回的数据类型
            success: function (updatemodalhtml) {
                content = updatemodalhtml;
                //弹出修改框
                $.TeachDialog({
                    title: '关联团队信息',
                    content: content,
                    showCloseButton: true,
                    largeSize:true,

                    showCloseButtonName: '关闭',
                    otherButtons: ['保存'],
                    otherButtonStyles: ['btn-primary'],
                    clickButton: function (sender, modal, index) {
                        // var formData = new FormData($("#updateForm" )[0]);
                        $.ajax({
                            url: 'super/Team/insertReTeam',
                            data: $("#updateForm").serialize(),
                            type: 'POST',
                            dataType: 'JSON',
                            // async: false,
                            // cache: false,
                            // contentType: false,
                            // processData: false,
                            success: function (returnData) {
                                if (returnData.status == 0) {
                                    // $.TeachDialog({
                                    //     content: returnData.msg
                                    // });

                                    layer.open({
                                        content: returnData.msg
                                    });
                                } else {
                                    modal.modal('hide');
                                    $('#datatable_noticeinfo').datagrid('reload');
                                }
                            },
                            error: function () {
                                $.TeachDialog({
                                    content: '系统异常，请联系管理员'
                                });
                            },

                        });

                    },
                });
            },
            error: function () {
                $.TeachDialog({
                    content: '系统异常，请联系管理员'
                });
                return;
            }
        });
    })

    /***
     * 解除关联团队
     */
    $('.relieve').click(function () {
        var row = $('#datatable_noticeinfo').datagrid('getSelections');
        if (row.length<1) {
            $.TeachDialog({
                content: '请选择一行数据进行解除关联！',
            });
            return;
        }
        if (row.length>1) {
            $.TeachDialog({
                content: '只能选择一行进行解除关联！',
            });
            return;
        }
        var content = "";
        $.ajax({
            url: 'super/Team/relieveTeam',
            data: "nid=" + row[0].team_id,
            type: 'POST',
            dataType: 'HTML',//返回的数据类型
            success: function (updatemodalhtml) {
                content = updatemodalhtml;
                //弹出修改框
                $.TeachDialog({
                    title: '解除关联团队信息',
                    content: content,
                    showCloseButton: true,
                    largeSize:true,

                    showCloseButtonName: '关闭',
                    otherButtons: ['保存'],
                    otherButtonStyles: ['btn-primary'],
                    clickButton: function (sender, modal, index) {
                        // var formData = new FormData($("#updateForm" )[0]);
                        $.ajax({
                            url: 'super/Team/insertRelieveTeam',
                            data: $("#updateForm").serialize(),
                            type: 'POST',
                            dataType: 'JSON',
                            // async: false,
                            // cache: false,
                            // contentType: false,
                            // processData: false,
                            success: function (returnData) {
                                if (returnData.status == 0) {
                                    // $.TeachDialog({
                                    //     content: returnData.msg
                                    // });
                                    layer.open({
                                        content: returnData.msg
                                    });
                                } else {
                                    modal.modal('hide');
                                    $('#datatable_noticeinfo').datagrid('reload');
                                }
                            },
                            error: function () {
                                $.TeachDialog({
                                    content: '系统异常，请联系管理员'
                                });
                            },

                        });

                    },
                });
            },
            error: function () {
                $.TeachDialog({
                    content: '系统异常，请联系管理员'
                });
                return;
            }
        });
    })

    /**
     * 添加团队成员
     */
    $('.member').click(function () {
        var row = $('#datatable_noticeinfo').datagrid('getSelections');
        if (row.length<1) {
            $.TeachDialog({
                content: '请选择一行数据进行添加团队成员！',
            });
            return;
        }
        if (row.length>1) {
            $.TeachDialog({
                content: '只能选择一行进行添加团队成员！',
            });
            return;
        }
        var content = "";
        $.ajax({
            url: 'super/Team/addTeamMember',
            data: "nid=" + row[0].team_id,
            type: 'POST',
            dataType: 'HTML',//返回的数据类型
            success: function (updatemodalhtml) {
                content = updatemodalhtml;
                //弹出修改框
                $.TeachDialog({
                    title: '添加团队成员信息',
                    content: content,
                    showCloseButton: true,
                    largeSize:true,

                    showCloseButtonName: '关闭',
                    otherButtons: ['保存'],
                    otherButtonStyles: ['btn-primary'],
                    clickButton: function (sender, modal, index) {
                        $.ajax({
                            url: 'super/Team/insertTeamMember',
                            data: $("#updateForm").serialize(),
                            type: 'POST',
                            dataType: 'JSON',
                            success: function (returnData) {
                                if (returnData.status == 0) {
                                    // $.TeachDialog({
                                    //     content: returnData.msg
                                    // });
                                    layer.open({
                                        content: returnData.msg
                                    });
                                } else {
                                    modal.modal('hide');
                                    $('#datatable_noticeinfo').datagrid('reload');
                                }
                            },
                            error: function () {
                                $.TeachDialog({
                                    content: '系统异常，请联系管理员'
                                });
                            },

                        });

                    },
                });
            },
            error: function () {
                $.TeachDialog({
                    content: '系统异常，请联系管理员'
                });
                return;
            }
        });
    })

    /**
     * 编辑团队成员
     */
    $('.editmember').click(function () {
        var row = $('#datatable_noticeinfo').datagrid('getSelections');
        if (row.length<1) {
            $.TeachDialog({
                content: '请选择一行数据进行编辑团队成员！',
            });
            return;
        }
        if (row.length>1) {
            $.TeachDialog({
                content: '只能选择一行进行编辑团队成员！',
            });
            return;
        }
        var content = "";
        $.ajax({
            url: 'super/Team/updateTeamMember',
            data: "nid=" + row[0].team_id,
            type: 'POST',
            dataType: 'HTML',//返回的数据类型
            success: function (updatemodalhtml) {
                content = updatemodalhtml;
                //弹出修改框
                $.TeachDialog({
                    title: '编辑团队成员信息',
                    content: content,
                    showCloseButton: true,
                    largeSize:true,

                    showCloseButtonName: '关闭',
                    otherButtons: ['保存'],
                    otherButtonStyles: ['btn-primary'],
                    clickButton: function (sender, modal, index) {
                        $.ajax({
                            url: 'super/Team/updateTeamMemberInfo',
                            data: $("#updateForm").serialize(),
                            type: 'POST',
                            dataType: 'JSON',
                            success: function (returnData) {
                                if (returnData.status == 0) {
                                    // $.TeachDialog({
                                    //     content: returnData.msg
                                    // });
                                    layer.open({
                                        content: returnData.msg
                                    });
                                } else {
                                    modal.modal('hide');
                                    $('#datatable_noticeinfo').datagrid('reload');
                                }
                            },
                            error: function () {
                                $.TeachDialog({
                                    content: '系统异常，请联系管理员'
                                });
                            },

                        });

                    },
                });
            },
            error: function () {
                $.TeachDialog({
                    content: '系统异常，请联系管理员'
                });
                return;
            }
        });
    })


});